<?php
/**
 * @var $model Term
 */
$cs = Yii::app()->clientScript;
$script = "
jQuery('#Term_name').bind('keyup change',function(){
    var text = jQuery(this).val();
    var alias = text
                .toLowerCase() // change everything to lowercase
                .replace(/^\s+|\s+$/g, '') // trim leading and trailing spaces
                .replace(/[_|\s]+/g, '-') // change all spaces and underscores to a hyphen
                .replace(/[^a-z0-9\-_]+/g, '') // remove all non-alphanumeric characters except the hyphen
                .replace(/[-]+/g, '-') // replace multiple instances of the hyphen with a single instance
                .replace(/^-+|-+$/g, '') // trim leading and trailing hyphens
                ;
    jQuery('#Term_alias').val(alias);
});
";
if (!$model->isNewRecord) //If the term is editting, then lock the key up event, add link generate-alias to generate alias manualy
    $script = "
jQuery('a.generate-alias').bind('click',function(){
        var text = jQuery('#Term_name').val();
        var alias = text
            .toLowerCase() // change everything to lowercase
            .replace(/^\s+|\s+$/g, '') // trim leading and trailing spaces
            .replace(/[_|\s]+/g, '-') // change all spaces and underscores to a hyphen
            .replace(/[^a-z0-9\-_]+/g, '') // remove all non-alphanumeric characters except the hyphen
            .replace(/[-]+/g, '-') // replace multiple instances of the hyphen with a single instance
            .replace(/^-+|-+$/g, '') // trim leading and trailing hyphens
            ;
        jQuery('#Term_alias').val(alias);
        return false;
    });
";

$cs->registerScript(__CLASS__ . '#UpdateAlias', $script);
unset($script);
?>

<style type="text/css">
    .form .form .note, .form .form .buttons {
        display: none;
    }
</style>
<div class="form wide">

    <?php $form = $this->beginWidget('CActiveForm', array(
    'id' => 'term-form',
    'enableAjaxValidation' => false,
)); ?>

    <?php echo $form->errorSummary($model); ?>

    <?php if (!$model->IsNewRecord) echo $form->hiddenField($model, "id"); ?>

    <?php echo $form->hiddenField($model, 'v_id'); ?>

    <div class="row">
        <?php echo $form->labelEx($model, 'parentId'); ?>
        <?php
        $model->children;
        $this->widget('ListTaxonomy', array(
            'model' => $model,
            'attribute' => 'parentId',
            'taxonomy' => $v,
            'module' => $module,
            'exclude' => $model->id ? array($model->id) : array(),
            'htmlOptions' => array('prompt' => 'Root'),
            'selected' => count($model->parent) ? $model->parent[0]->id : 0,
        ));?>
        <?php echo $form->error($model, 'parentId'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'name'); ?>
        <?php echo $form->textField($model, 'name', array('size' => 60, 'maxlength' => 255)); ?>
        <?php echo $form->error($model, 'name'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'alias'); ?>
        <?php echo $form->textField($model, 'alias', array('size' => 60, 'maxlength' => 255)); ?>
        <?php if (!$model->isNewRecord): ?>
        <span
            style="float: none;"><?php echo CHtml::link('Generate alias', '#', array('class' => 'generate-alias'))?></span>
        <?php endif;?>
        <?php echo $form->error($model, 'alias'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'description'); ?>
        <?php echo $form->textArea($model, 'description', array('rows' => 6, 'cols' => 50)); ?>
        <?php echo $form->error($model, 'description'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'state'); ?>
        <?php echo $form->dropDownList($model, 'state', $model->stateOptions); ?>
        <?php echo $form->error($model, 'state'); ?>
    </div>

    <?php if (is_object($property)): ?>
    <?php
    $propertyClassName[0] = strtolower($propertyClassName[0]);
    $propertyForm = $this->renderPartial($propertyModule . '.views.admin.' . $propertyClassName . '._form', array('model' => $property), true);
    echo strip_tags($propertyForm, '<div><label><textarea><input><select><p>')
    ?>
    <?php endif;?>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
        <?php if (Yii::app()->user->hasFlash('category_just_added')) : ?>
        <a href="<?php echo $this->createUrl('/Admin/term/update', array('id' => Yii::app()->user->getFlash('category_just_added'), 'v' => $v, 'm' => $module)); ?>">Edit
            the category just added</a>
        <?php endif; ?>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->